package m.system.util;

import m.system.RuntimeData;

import java.io.*;
import java.net.URL;
import java.nio.charset.StandardCharsets;


public class FileUtil {
	/**
	 * 判断路径是否存在
	 */
	public static boolean existsPublicFile(String url_path){
		File file=new File(RuntimeData.getPublicWeb()+url_path);
		return file.exists();
	}
	public static boolean existsWebFile(String url_path){
		File file=new File(RuntimeData.getClassWeb()+url_path);
		return file.exists();
	}

	/**
	 * 获取公共目录相对路径文件
	 */
	public static File getPublicFile(String url_path) throws IOException{
		return getFile(RuntimeData.getPublicWeb()+url_path);
	}
//	public static File getWebFile(String url_path) throws IOException{
//		return getFile(RuntimeData.getClassWeb()+url_path);
//	}

	/**
	 * 写入公共目录相对路径文件
	 */
	public static void writePublicFile(String url_path,InputStream inStream) throws Exception{
		writeFile(RuntimeData.getPublicWeb()+url_path,inStream);
	}
//	public static void writeWebFile(String url_path,InputStream inStream) throws Exception{
//		writeFile(RuntimeData.getClassWeb()+url_path,inStream);
//	}
	/**
	 * 写入公共目录相对路径文件
	 */
	public static void writePublicFile(String url_path,File file) throws Exception{
		writeFile(RuntimeData.getPublicWeb()+url_path,file);
	}
//	public static void writeWebFile(String url_path,File file) throws Exception{
//		writeFile(RuntimeData.getClassWeb()+url_path,file);
//	}
	/**
	 * 删除公共目录相对路径文件
	 */
	public static boolean deletePublicFile(String url_path) {
		File file=new File(RuntimeData.getPublicWeb()+url_path);
		if(file.exists()&&file.isFile()){
			return file.delete();
		}else{
			return false;
		}
	}
//	public static boolean deleteWebFile(String url_path) throws IOException{
//		File file=new File(RuntimeData.getClassWeb()+url_path);
//		if(file.exists()&&file.isFile()){
//			return file.delete();
//		}else{
//			return false;
//		}
//	}
	public static File getDirs(String path){
		File file=new File(path);
		if(!file.exists()){
			file.mkdirs();
		}
		return file;
	}
	public static File getFile(String path) throws IOException{
		File file=new File(path);
		if(!file.exists()){
			getDirs(file.getPath().substring(0, file.getPath().lastIndexOf(file.getName())));
			file.createNewFile();
		}
		return file;
	}
	public static File getNetworkTempFile(String urlPath,String suffix){
		URL url;
		try {
			url = new URL(urlPath);
			File f=FileUtil.getFile(RuntimeData.getClassTmp() + "tmp/" + GenerateID.tempKey() + "." + suffix);
			writeFile(f.getPath(), url.openStream());
			return f;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	public static String read(File file) throws IOException{
		StringBuilder result = new StringBuilder();
		InputStreamReader read = new InputStreamReader(new FileInputStream(file.getPath()), StandardCharsets.UTF_8);
		BufferedReader br = new BufferedReader(read);
		String s;
		while ((s = br.readLine()) != null) {
			result.append(System.lineSeparator()).append(s);
		}
		br.close();
		return result.toString();
	}
	public static boolean deleteFile(String path) throws IOException{
		File file=new File(path);
		if(file.exists()&&file.isFile()){
			return file.delete();
		}else{
			return false;
		}
	}
	public static void writeFile(String filePath,File file) throws Exception {
		writeFile(filePath,new FileInputStream(file.getPath()));
	}
	public static void writeFile(String filePath,InputStream inStream) throws Exception{
		Exception e1=null;
		try (OutputStream outSteam = new FileOutputStream(getFile(filePath))) {
			byte[] buffer = new byte[1024];
			int len;
			while ((len = inStream.read(buffer)) != -1) {
				outSteam.write(buffer, 0, len);
			}
		} catch (Exception e) {
			e1 = e;
		} finally {
			if (null != inStream)
				inStream.close();
		}
		if(null!=e1) throw e1;
	}
}
